import {getCache, setCache} from '../utils/cache'

const USER_INFO_KEY = 'USER_INFO_KEY'
let res = getCache(USER_INFO_KEY)
res = res ? res : {}
const USER_STATE = {...res}

export default function user(prestate=USER_STATE, action) {
  switch(action.type) {
    case 'loginSuccess':
      let successData = {...prestate, accessToken: action.accesstoken, loginname: action.loginname, avatar_url: action.avatar_url}
      setCache(USER_INFO_KEY, successData)
      return successData
    case 'loginFail': 
      let failData = {...prestate, accessToken: action.accesstoken, loginname: action.loginname, avatar_url: action.avatar_url}
      setCache(USER_INFO_KEY, failData)
      return failData
    default:
      return {...prestate}
  }
}